// 购物车
import { findHeadCategory } from '@/api/home'
import { category } from '@/utils/constant.js'

export default {
  namespaced: true,
  // 另一种写法
  state: () => {
    return {
      list: category.map((item, index) => ({ id: index, name: item }))
    }
  },
  // state: {
  //   list: []
  // },
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    // 控制二级分类隐藏
    hide (state, id) {
      const cate = state.list.find(item => item.id === id)
      if (cate) {
        cate.open = false
      }
    },
    // 控制二级分类显示
    show (state, id) {
      const cate = state.list.find(item => item.id === id)
      if (cate) {
        cate.open = open
      }
    }
  },
  actions: {
    async getList (cxt) {
      try {
        const res = await findHeadCategory()
        res.result.forEach(item => {
          item.open = false
        })
        cxt.commit('setList', res.result)
      } catch (error) {
        alert(error)
      }
    }
  },
  getters: {}
}
